package com.way.util;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.way.page.Page;
import com.way.page.TableSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {

    public static void startPage() {
        Page page = TableSupport.buildPageRequest();
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();
        if (null != pageNum && null != pageSize) {
            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
            PageHelper.startPage(pageNum, pageSize);
        }
    }

    public static Map<String, Object> pageResult(List<?> list) {
        PageInfo<?> pageInfo = new PageInfo(list);
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("rows", list);
        m.put("pageNum", pageInfo.getPageNum());
        m.put("total", pageInfo.getTotal());
        return m;
    }
}
